package com.aqie.easy.dfs;

import com.aqie.easy.structure.Employee;

import java.util.ArrayList;
import java.util.List;

/**
 * 690 员工的重要性
 * todo 并发异常
 * ArrayList 初始化
 * 1. ArrayList<Type> obj = new ArrayList<Type>(Arrays.asList())
 * 2. ArrayList<T> obj = new ArrayList<T>() {{ add(Object o1);}}
 * 3.
 * 4. ArrayList<T> obj = new ArrayList<T>(Collections.nCopies(count,element))
 */
public class GetImportance {
    private int ans = 0;
    public int getImportance(List<Employee> employees, int id) {

        for (Employee e: employees) {
            if (e.id == id){
                ans += e.importance;
                // System.out.println(e.subordinates);
                if (!e.subordinates.isEmpty()){
                    List<Integer> sub = e.subordinates;
                    // employees.remove(e);
                    for (int i = 0; i < sub.size(); i++){
                        getImportance(employees, sub.get(i));
                    }
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Employee e1 = new Employee();
        e1.id = 1;
        e1.importance = 5;
        e1.subordinates = new ArrayList<Integer>(){{
            add(2);
            add(3);
        }};

        Employee e2 = new Employee();
        e2.id = 2;
        e2.importance = 3;
        e2.subordinates = new ArrayList<>();

        Employee e3 = new Employee();
        e3.id = 3;
        e3.importance = 3;
        e3.subordinates = new ArrayList<>();


        List<Employee> employees = new ArrayList<>();
        employees.add(e1);
        employees.add(e3);
        employees.add(e2);

        System.out.println(new GetImportance().getImportance(employees, 1));

    }


}
